<?php 

use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration as DoctrineConfiguration,
Doctrine\Common\EventManager as DoctrineEventManager,
Doctrine\ORM\EntityManager as DoctrineEntityManager,
Doctrine\Common\Cache\ApcCache as DoctrineApcCache,
Doctrine\Common\Cache\ArrayCache as DoctrineArrayCache;

class Admin_BootStrap extends Zend_Application_Module_Bootstrap
{
	
	protected function _initloadModuleConfig()
	{
		
		// would probably better to use 
		// Zend_Controller_Front::getModuleDirectory() ?
		$configFile = APPLICATION_PATH 
			. '/modules/' 
			. '/admin/' 
			. '/configs/module.ini';
 
		if (!file_exists($configFile)) {
			return;
		}
		$config = new Zend_Config_Ini($configFile, $this->getEnvironment());
		$this->setOptions($config->toArray());
	}
	
	protected function _initAutoload()
	{
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$resourceloader= new Zend_Loader_Autoloader_Resource(array(
		'basePath'=>APPLICATION_PATH.'\\modules\\admin',
		'namespace'=>'Admin',
		'resourceTypes'=> array('form'=>array('path'=>'forms/','namespace'=>'Form'),
		'model'=>array('path'=>'models/','namespace'=>'Model'))
		));
		$autoloader->pushAutoloader($resourceloader);
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$auth = Zend_Auth::getInstance();
		    $infoUser = $auth->getIdentity();
		    Zend_Registry::set('fullname', $infoUser->full_name);
		}
		
	}
	protected function _initView()
	{    
	   // Initialize view 	
	   $view = new Zend_View();
		
	   $view->doctype('HTML5');
	  	   
	    $view->headTitle('Hoian Express'); 	 
	    $view->headTitle()->setSeparator(' - ');
	    $view->headTitle("Admin"); 

	    $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
	    $view->jQuery()->enable();
		$view->jQuery()->uiEnable();
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
    	$viewRenderer->setView($view); 
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); 
		
		/**
		 * Setup the Custom Helpers
		 */
		Zend_Controller_Action_HelperBroker::addPrefix('HAE_Helper');	   
	   
		//HEADLINK
		$view->headLink()->appendStylesheet('/css/admin/admin.css');
		
		//-JQuery
		$view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
		$view->jQuery()->enable();
		$view->jQuery()->uiEnable();
		$view->jQuery()->setLocalPath('/js/library/jquery/js/jquery-1.6.4.js')
		->setUILocalPath('/js/library/jquery/js/jquery-ui-1.8.16.custom.min.js')
		//->addJavascriptFile('/js/default/base.js')
		->addJavascriptFile('/js/default/rotate3di.js');
		
		// OTHER JS
		$view->headScript()->appendFile('/js/library/jquery/js/jquery.alerts.js');
		$view->headScript()->appendFile('/js/library/modernizr.js');		
		$view->headScript()->appendFile('/js/library/tiny_mce/tiny_mce.js');
		$view->headScript()->appendFile('/js/library/tiny_mce/tiny_mce-init.js');
		$view->headScript()->appendFile('/js/admin/all.js');
		$view->headScript()->appendFile('/js/admin/popup.js');
		$view->headScript()->appendFile('/js/library/validate.js');
	}
	
	
	protected function _initZFDebug()
	 {
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->registerNamespace('ZFDebug');
		
		$options = array(
			'plugins' => array('Variables', 
							   'File' => array('base_path' => '/'),
							   'Memory', 
							   'Time', 
							   'Registry', 
							   'Exception')
		);
		
		# Instantiate the database adapter and setup the plugin.
		# Alternatively just add the plugin like above and rely on the autodiscovery feature.
		if ($this->hasPluginResource('db')) {
			$this->bootstrap('db');
			$db = $this->getPluginResource('db')->getDbAdapter();
			$options['plugins']['Database']['adapter'] = $db;
		}
//	
		# Setup the cache plugin
		if ($this->hasPluginResource('cache')) {
			$this->bootstrap('cache');
			$cache = $this->getPluginResource('cache')->getDbAdapter();
			$options['plugins']['Cache']['backend'] = $cache->getBackend();
		}
	
// 		$debug = new ZFDebug_Controller_Plugin_Debug($options);		
// 		$this->bootstrap('frontController');
// 		$frontController = $this->getResource('frontController');
// 		$frontController->registerPlugin($debug);
	}	
	
	/**
	 * Initializes and returns Doctrine ORM entity manager
	 *
	 * @return \Doctrine\ORM\EntityManager	
	 */
	
	public function _initClassLoaders()
	{
		//Doctrine loader
		require_once (APPLICATION_PATH .
				DIRECTORY_SEPARATOR . '..' .
				DIRECTORY_SEPARATOR . 'library' .
				DIRECTORY_SEPARATOR . 'Doctrine' .
				DIRECTORY_SEPARATOR . 'Common' .
				DIRECTORY_SEPARATOR . 'ClassLoader.php'
		);
	
		$doctrineAutoloader = new \Doctrine\Common\ClassLoader('Doctrine', APPLICATION_PATH .
				DIRECTORY_SEPARATOR . '..' .
				DIRECTORY_SEPARATOR . 'library'
		);
		$doctrineAutoloader->register();
	
		//Add an autonamespace for the entities
		$serviceAutoloader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH .
				DIRECTORY_SEPARATOR . '..' .
				DIRECTORY_SEPARATOR . 'doctrine'
		);
		$serviceAutoloader->register();
	
		//Add an autonamespace for the repositories
		$serviceAutoloader = new \Doctrine\Common\ClassLoader('Repositories', APPLICATION_PATH .
				DIRECTORY_SEPARATOR . '..' .
				DIRECTORY_SEPARATOR . 'doctrine'
		);
		$serviceAutoloader->register();
	
		//Add an autonamespace for the services
		$serviceAutoloader = new \Doctrine\Common\ClassLoader('Services', APPLICATION_PATH .
				DIRECTORY_SEPARATOR . '..' .
				DIRECTORY_SEPARATOR . 'doctrine'
		);
		$serviceAutoloader->register();
	
		//Doctrine Extensions library
		$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', APPLICATION_PATH .
				DIRECTORY_SEPARATOR . '..' .
				DIRECTORY_SEPARATOR . 'library'
		);
		$classLoader->register();
	
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->pushAutoloader(array($serviceAutoloader, 'loadClass'), 'Entities');
		$autoloader->pushAutoloader(array($serviceAutoloader, 'loadClass'), 'Repositories');
	
	
	
	}
	
	/**
	 * Initializes and returns Doctrine ORM entity manager
	 *
	 * @return \Doctrine\ORM\EntityManager
	 * @todo Resource configurator like http://framework.zend.com/wiki/x/0IAbAQ
	 */
	
	protected function _initDoctrine()
	{
	
		$this->bootstrap(array('classLoaders', 'doctrineCache'));
	
		// Parameters in .ini files
		$zendConfig = $this->getOptions();
	
		// Database : driver, host, user, password and dbname
		$connectionParameters = $zendConfig['doctrine']['connection'];
	
		// Configuration : now initialize the configuration object
		$configuration = new DoctrineConfiguration();
	
		// Metatada tags : used to avoid parsing all mapping information every time the framework is initialized.
		$configuration->setMetadataCacheImpl($this->getResource('doctrineCache'));
	
		// For performance reasons, it is also recommended to use a result cache
		$configuration->setResultCacheImpl($this->getResource('doctrineCache'));
	
		// If true, Doctrine 2 will generate proxy classes for your entities on the fly. Reduce performance and should be disabled in the production
		$configuration->setAutoGenerateProxyClasses($zendConfig['doctrine']['autoGenerateProxyClasses']);
	
		// The directory, where your proxy classes live
		$configuration->setProxyDir($zendConfig['doctrine']['proxyPath']);
	
		// The proxy classes' namespace
		$configuration->setProxyNamespace($zendConfig['doctrine']['proxyNamespace']);
	
		// Language description we want to use for the mapping information
		$configuration->setMetadataDriverImpl(
				$configuration->newDefaultAnnotationDriver(
						$zendConfig['doctrine']['entityPath']));
			
		// Manager : create an event manager
		$eventManager = new DoctrineEventManager();
	
		// now we have everything required to initialize the entity manager
		$entityManager = DoctrineEntityManager::create($connectionParameters, $configuration, $eventManager);
	
		Zend_Registry::set('doctrine', $entityManager);
	
		//$this->_registry->doctrine      = new stdClass();
		//$this->_registry->doctrine->_em = DoctrineEntityManager::create($connectionParameters, $configuration, $eventManager);
	
		return $entityManager;
	}
	
	/**
	 * @return Doctrine\Common\CacheProvider
	 */
	public function _initDoctrineCache()
	{
		$cache = null;
		if (APPLICATION_ENV === 'development') {
			$cache = new DoctrineArrayCache();
		} else {
			$cache = new DoctrineApcCache();
		}
		return $cache;
	}
	
}
